package com.cmo.rzl.triage;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.beetl.sql.core.kit.StringKit;

import com.cmo.rzl.core.base.BaseController;
import com.cmo.rzl.core.jfinal.ext.autoroute.ControllerBind;
import com.cmo.rzl.core.toolbox.Record;
import com.cmo.rzl.core.toolbox.kit.ShardKit;
import com.cmo.rzl.shine.model.PatientQueue;
import com.cmo.rzl.shine.model.Triage;
import com.cmo.rzl.shine.service.DoctorService;
import com.cmo.rzl.shine.service.PatientQueueService;
import com.cmo.rzl.shine.service.TriageService;
import com.cmo.rzl.shine.service.impl.DoctorServiceImpl;
import com.cmo.rzl.shine.service.impl.PatientQueueServiceImpl;
import com.cmo.rzl.shine.service.impl.TriageServiceImpl;
import com.jfinal.kit.StrKit;

@ControllerBind(controllerKey = "/clientPatientQueue")
public class ClientPatientQueueController extends BaseController {

	PatientQueueService service = new PatientQueueServiceImpl();
	ShardKit shardkit = new ShardKit();
	TriageService triage = new TriageServiceImpl();
	DoctorService doctor = new DoctorServiceImpl();

	// 手动录入患者
	public void Manual() {
		Map<String, Object> map = new HashMap<String, Object>();
		// 患者姓名
		String patientName = getPara("patientName");
		if (StringKit.isBlank(patientName)) {
			renderJson(error(NOT_NULL_MSG));
			return;
		}
		// 队列列表
		String QueueNumber = getPara("QueueNumber");
		if (StringKit.isBlank(QueueNumber)) {
			renderJson(error(NOT_NULL_MSG));
			return;
		}
		// 查询序号的最大值
		PatientQueue record = service.findByQueueNum(QueueNumber);
		System.out.println(record.getRegister_id());
		PatientQueue patientqueue = new PatientQueue();
		patientqueue.setPatient_name(patientName);
		patientqueue.setQueue_type_id(Integer.parseInt(QueueNumber));
		patientqueue.setRegister_id(record.getRegister_id());
		patientqueue.setQueue_num(record.getRegister_id());
		patientqueue.setState_patient(0);
		patientqueue.setSub_queue_order(0);
		patientqueue.setIs_display(2);
		patientqueue.setIs_deleted(0);
		patientqueue.setTime_interval(0);
		patientqueue.setPatient_id(new SimpleDateFormat("yyyyMMddhhmmss")
				.format(new Date()));
		patientqueue
				.setOpr_time(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")
						.format(new Date()));
		Boolean bool = service.save(patientqueue);
		if (!bool) {
			map.put("return_msg", SAVE_FAIL_MSG);
			map.put("return_code", "fail");
			renderJson(map);
			return;
		} else {
			map.put("return_msg", SAVE_SUCCESS_MSG);
			map.put("return_code", "success");
			renderJson(map);
		}
	}

	// 患者列表
	public void listPatient_wait() {
		Map<String, Object> map = new HashMap<String, Object>();
		// 获取队列编号
		String QueueNumber = getPara("queue_number");
		if (StringKit.isBlank(QueueNumber)) {
			renderJson(error(NOT_NULL_MSG));
			return;
		}
		// 查询患者
		List<Record> list = service.listPatient(QueueNumber);
		if (null == list) {
			renderJson(error(QUERY_FAIL_MSG));
			return;
		} else {
			map.put("return_msg", QUERY_SUCCESS_MSG);
			map.put("return_code", "success");
			map.put("count", list.size());
			map.put("list", list);
			renderJson(map);
		}
	}

	// 未到过号患者
	public void listPatient_pass() {
		Map<String, Object> map = new HashMap<String, Object>();
		// 获取队列编号
		String QueueNumber = getPara("queue_number");
		if (StringKit.isBlank(QueueNumber)) {
			renderJson(error(NOT_NULL_MSG));
			return;
		}
		// 查询患者
		List<Record> list = service.listPatient_pass(QueueNumber);
		if (null == list) {
			renderJson(error(QUERY_FAIL_MSG));
			return;
		} else {
			map.put("return_msg", QUERY_SUCCESS_MSG);
			map.put("return_code", "success");
			map.put("count", list.size());
			map.put("list", list);
			renderJson(map);
		}
		renderJson(map);
	}

	// 已就诊
	public void listPatient_already() {
		Map<String, Object> map = new HashMap<String, Object>();
		// 获取队列编号
		String QueueNumber = getPara("queue_number");
		if (StringKit.isBlank(QueueNumber)) {
			renderJson(error(NOT_NULL_MSG));
			return;
		}
		// 查询患者
		List<Record> list = service.listPatient_already(QueueNumber);
		if (null == list) {
			renderJson(error(QUERY_FAIL_MSG));
			return;
		} else {
			map.put("return_msg", QUERY_SUCCESS_MSG);
			map.put("return_code", "success");
			map.put("count", list.size());
			map.put("list", list);
			renderJson(map);
		}
		renderJson(map);
	}

	// 过号患者
	public void listPatient_passno() {
		Map<String, Object> map = new HashMap<String, Object>();
		// 获取队列编号
		String QueueNumber = getPara("queue_number");
		if (StringKit.isBlank(QueueNumber)) {
			renderJson(error(NOT_NULL_MSG));
			return;
		}
		// 查询患者
		List<Record> list = service.listPatient_passno(QueueNumber);
		if (null == list) {
			renderJson(error(QUERY_FAIL_MSG));
			return;
		} else {
			map.put("return_msg", QUERY_SUCCESS_MSG);
			map.put("return_code", "success");
			map.put("count", list.size());
			map.put("list", list);
			renderJson(map);
		}
		renderJson(map);
	}

	// 优先
	public void patientFirst() {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = getPara("ids");
		if (StringKit.isBlank(ids)) {
			renderJson(error(NOT_NULL_MSG));
			return;
		}
		ids = ids.substring(0, ids.length() - 1);
		boolean bool = service.patientFirst(ids);
		if (bool) {
			map.put("return_msg", SETTING_SUCCESS_MSG);
			map.put("return_code", "success");
			renderJson(map);
		} else {
			map.put("return_msg", SETTING_ERROR_MSG);
			map.put("return_code", "fail");
			renderJson(map);
			return;
		}
	}

	// 扫描报道添加
	public void ScanAdd() {
		Map<String, Object> map = new HashMap<String, Object>();
		String code = getPara("code");
		if (StringKit.isBlank(code)) {
			renderJson(error(NOT_NULL_MSG));
			return;
		}
		String ip = shardkit.getIpAddr(getRequest());
		if (StringKit.isBlank(ip)) {
			renderJson(error(NOT_NULL_MSG));
			return;
		}

		// 根据code 查询患者
		Record record = service.findCode(code, ip);
		if (null == record) {
			renderJson(error(QUERY_FAIL_MSG));
			return;
		}
		String is_display = record.getInt("is_display").toString();
		// 未报道患者
		if ("1".equals(is_display)) {
			int disp = service.updatepatientqueuedislpay(code);
			if (disp > 0) {
				map.put("return_msg", "当前患者:" + record.getStr("patient_name")
						+ "已成功报道在当前分诊台" + record.getStr("triage_name") + "下");
				map.put("return_code", "success");
				map.put("queue_type_id", record.getInt("queue_type_id"));
				map.put("id", record.getInt("id"));
				map.put("state_patient", record.get("state_patient"));
				renderJson(map);
			} else {
				map.put("return_msg", "患者报道失败");
				map.put("return_code", "fail");
				renderJson(map);
				return;
			}
		} else if ("2".equals(is_display)) {// 已经为报道的患者
			map.put("return_msg", "当前患者" + record.getStr("patient_name")
					+ "在队列:" + record.getStr("queue_type_name") + "中");
			map.put("return_code", "success");
			map.put("queue_type_id", record.getInt("queue_type_id"));
			map.put("id", record.getInt("id"));
			map.put("state_patient", record.get("state_patient"));
			renderJson(map);
			return;
		} else {
			// 其他错误原因
			renderJson(error(QUERY_FAIL_MSG));
			return;
		}
	}

	// 初诊
	public void newlyDiagnosed() {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = getRequest().getParameter("ids");
		if (StringKit.isBlank(ids)) {
			renderJson(error(NOT_NULL_MSG));
			return;
		}
		ids = ids.substring(0, ids.length() - 1);
		boolean bool = service.NewlyDiagnosed(ids);
		if (bool) {
			map.put("return_msg", UPDATE_SUCCESS_MSG);
			map.put("return_code", "success");
			renderJson(map);
		} else {
			map.put("return_code", "fail");
			map.put("return_msg", UPDATE_FAIL_MSG);
			renderJson(map);
			return;
		}
	}

	// 过号
	public void passed() {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = getRequest().getParameter("ids");
		if (StringKit.isBlank(ids)) {
			renderJson(error(NOT_NULL_MSG));
			return;
		}
		ids = ids.substring(0, ids.length() - 1);

		boolean bool = service.passed(ids);
		if (bool) {
			map.put("return_msg", UPDATE_SUCCESS_MSG);
			map.put("return_code", "success");
			renderJson(map);
		} else {
			map.put("return_msg", UPDATE_FAIL_MSG);
			map.put("return_code", "fail");
			renderJson(map);
			return;
		}
	}

	// 复诊
	public void visit() {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = getPara("ids");
		if (StringKit.isBlank(ids)) {
			renderJson(error(NOT_NULL_MSG));
			return;
		}
		ids = ids.substring(0, ids.length() - 1);
		boolean bool = service.visit(ids);
		if (bool) {
			map.put("return_msg", UPDATE_SUCCESS_MSG);
			map.put("return_code", "success");
			renderJson(map);
		} else {
			map.put("return_msg", UPDATE_FAIL_MSG);
			map.put("return_code", "fail");
			renderJson(map);
			return;
		}
	}

	// 绿色通道
	public void GreenChannel() {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = getRequest().getParameter("ids");
		if (StringKit.isBlank(ids)) {
			renderJson(error(NOT_NULL_MSG));
			return;
		}
		ids = ids.substring(0, ids.length() - 1);

		boolean bool = service.GreenChannel(ids);
		if (bool) {
			map.put("return_msg", UPDATE_SUCCESS_MSG);
			map.put("return_code", "success");
			renderJson(map);
		} else {
			map.put("return_msg", UPDATE_FAIL_MSG);
			map.put("return_code", "fail");
			renderJson(map);
			return;
		}
	}

	// 患者延迟
	public void delay() {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = getPara("ids");
		if (StringKit.isBlank(ids)) {
			renderJson(error(NOT_NULL_MSG));
			return;
		}
		ids = ids.substring(0, ids.length() - 1);

		int times = getParaToInt("timeInterval", -1);

		if (times == -1) {
			map.put("return_msg", NOT_NULL_MSG);
			map.put("return_code", "fail");
			renderJson(map);
			return;
		}
		boolean bool = service.delay(times, ids);
		if (bool) {
			map.put("return_msg", UPDATE_SUCCESS_MSG);
			map.put("return_code", "success");
			renderJson(map);
		} else {
			map.put("return_msg", UPDATE_FAIL_MSG);
			map.put("return_code", "fail");
			renderJson(map);
			return;
		}
	}

	// 挂起
	public void Hang() {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = getPara("ids");

		if (StringKit.isBlank(ids)) {
			renderJson(error(NOT_NULL_MSG));
			return;
		}
		ids = ids.substring(0, ids.length() - 1);

		boolean bool = service.Hang(ids);
		if (bool) {
			map.put("return_msg", UPDATE_SUCCESS_MSG);
			map.put("return_code", "success");
			renderJson(map);
		} else {
			map.put("return_msg", UPDATE_FAIL_MSG);
			map.put("return_code", "fail");
			renderJson(map);
			return;
		}
	}

	// 患者转诊
	public void referralQueueType() {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = getPara("ids");
		if (StringKit.isBlank(ids)) {
			renderJson(error(NOT_NULL_MSG));
			return;
		}
		ids = ids.substring(0, ids.length() - 1);

		String queue_type_id = getPara("queue_type_id");
		if (StringKit.isBlank(queue_type_id)) {
			map.put("return_msg", "请选择需要转诊的队列");
			map.put("return_code", "fail");
			renderJson(map);
			return;
		}
		String doctor_id = getPara("id");
		if (StringKit.isBlank(doctor_id)) {
			map.put("return_msg", "请选择医生");
			map.put("return_code", "fail");
			renderJson(map);
			return;
		}
		Record record = service.findmaxregisterid(queue_type_id);
		System.out
				.println(record + "**********" + record.getStr("register_id"));
		String register_id = null;
		if (StringKit.isBlank(record.getStr("register_id"))) {
			register_id = "转诊-1";
		} else {
			register_id = record.getStr("register_id");
			String[] a = register_id.split("转诊-");
			register_id = a[1];
			int maxnum = Integer.parseInt(register_id) + 1;
			register_id = "转诊-" + String.valueOf(maxnum);
		}
		boolean bool = service.ReferralQueueType(queue_type_id, ids,
				register_id, doctor_id);
		if (bool) {
			map.put("return_msg", "转诊患者成功");
			map.put("return_code", "success");
			renderJson(map);
		} else {
			map.put("return_msg", "转诊患者失败");
			map.put("return_code", "fail");
			renderJson(map);
			return;
		}
	}

	// 分诊
	public void examination() {
		Map<String, Object> map = new HashMap<String, Object>();
		// 获取患者id
		String ids = getPara("ids");
		if (StringKit.isBlank(ids)) {
			map.put("return_msg", "请选择需要操作的患者");
			map.put("return_code", "fail");
			renderJson(map);
			return;
		}
		ids = ids.substring(0, ids.length() - 1);

		// 根据分诊台 客户端ip查询分诊台ip
		String triage_ip = shardkit.getIpAddr(getRequest());
		Triage rt = triage.queryTriageIp(triage_ip);
		if (null == rt) {
			map.put("return_code", "fail");
			map.put("return_msg", "查询分诊类型失败");
			renderJson(map);
			return;
		}
		// 获取分诊台客服端所在的分针模式1医生 2叫号器
		String type = rt.getTriage_type().toString();

		// 获取前台传过来的队列id
		String queue_type_id = getPara("queue_type_id");
		if (StringKit.isBlank(queue_type_id)) {
			map.put("return_msg", "队列id不能为空");
			map.put("return_code", "fail");
			renderJson(map);
			return;
		}

		if ("1".equals(type)) {// 获取医生
			String doctor_id = getPara("id").toString();
			if (StringKit.isBlank(doctor_id)) {
				map.put("return_msg", "获取医生失败");
				map.put("return_code", "fail");
				renderJson(map);
				return;
			} else {
				boolean bool = service.examinationdoctor(doctor_id, ids,
						queue_type_id);
				if (bool) {
					map.put("return_msg", "分诊成功");
					map.put("return_code", "success");
					renderJson(map);
				} else {
					map.put("return_msg", "分诊失败");
					map.put("return_code", "fail");
					renderJson(map);
					return;
				}
			}
		} else if ("2".equals(type)) {// 获取叫号器
			String pager_id = getPara("id");
			if (StringKit.isBlank(pager_id)) {
				map.put("return_msg", "获取队列获取叫号器失败");
				map.put("return_code", "fail");
				renderJson(map);
				return;
			} else {
				boolean bool = service.examinationpager(pager_id, ids,
						queue_type_id);
				if (bool) {
					map.put("return_msg", "分诊成功");
					map.put("return_code", "fail");
					renderJson(map);
				} else {
					map.put("return_msg", "分诊失败");
					map.put("return_code", "fail");
					renderJson(map);
					return;
				}
			}
		}
	}

	// 取消分诊
	public void noexamination() {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = getPara("ids");
		if (StringKit.isBlank(ids)) {
			map.put("return_msg", "请选择需要操作的患者");
			map.put("return_code", "fail");
			renderJson(map);
			return;
		}
		ids = ids.substring(0, ids.length() - 1);
		// 根据分诊台 客户端ip查询分诊台ip
		String triage_ip = shardkit.getIpAddr(getRequest());
		Triage rt = triage.queryTriageIp(triage_ip);
		if (null == rt) {
			map.put("return_code", "fail");
			map.put("return_msg", "查询分诊类型失败");
			renderJson(map);
			return;
		}
		// 获取分诊台客服端所在的分针模式1医生 2叫号器
		String type = rt.getTriage_type().toString();

		boolean bool = service.noexamination(ids, type);
		if (bool) {
			map.put("return_msg", "取消分诊成功");
			map.put("return_code", "success");
			renderJson(map);
		} else {
			map.put("return_msg", "取消分诊失败");
			map.put("return_code", "fail");
			renderJson(map);
			return;
		}
	}

	// 根据分诊台的ip查询分诊类型 返回医生信息与叫号器信息
	public void list_queue_type() {
		Map<String, Object> map = new HashMap<String, Object>();
		// 根据分诊台 客户端ip查询分诊台ip
		String triage_ip = shardkit.getIpAddr(getRequest());
		Triage rt = triage.queryTriageIp(triage_ip);
		if (null == rt) {
			map.put("return_code", "fail");
			map.put("return_msg", "查询分针类型失败");
			renderJson(map);
			return;
		}
		List<Record> list = triage.queryByQueueType(triage_ip);
		if (null == list) {
			map.put("return_code", "fail");
			map.put("return_msg", "查询失败");
			renderJson(map);
			return;
		} else {
			map.put("return_code", "success");
			map.put("return_msg", "医生队列信息成功");
			map.put("list", list);
			map.put("count", list.size());
			renderJson(map);
		}
	}

	// 查询队列下的排班医生
	public void doctorId() {
		Map<String, Object> map = new HashMap<String, Object>();
		String queue_type_id = getPara("queue_type_id");
		if (StringKit.isBlank(queue_type_id)) {
			map.put("return_msg", "队列id不能为空");
			map.put("return_code", "fail");
			renderJson(map);
			return;
		}
		// 根据分诊台 客户端ip查询分诊台ip
		String triage_ip = shardkit.getIpAddr(getRequest());
		Triage rt = triage.queryTriageIp(triage_ip);
		if (null == rt) {
			map.put("return_code", "fail");
			map.put("return_msg", "查询分诊类型失败");
			renderJson(map);
			return;
		}
		// 获取分诊台客服端所在的分针模式1医生 2叫号器
		String type = rt.getTriage_type().toString();

		// 根据队列查询医生
		List<Record> list = service.finddoctorid(queue_type_id, type);
		if (null != list) {
			map.put("return_code", "success");
			map.put("return_msg", "当前队列排班医生信息查询成功");
			map.put("list", list);
			map.put("count", list.size());
			renderJson(map);
		} else {
			map.put("return_code", "fail");
			map.put("return_msg", "当前队列排班叫号器信息查询失败");
			renderJson(map);
			return;
		}
	}

	// 屏呼叫
	public void list_patient_call() {
		Map<String, Object> map = new HashMap<String, Object>();
		String ip = shardkit.getIpAddr(getRequest());
		if (StringKit.isBlank(ip)) {
			map.put("return_code", "fail");
			map.put("return _msg", "获取IP失败");
			renderJson(map);
			return;
		}
		List<Record> list = service.list_patient_call(ip);
		String[] aa = new String[0];
		if (list != null && !list.isEmpty()) {
			map.put("return_code", "success");
			map.put("return_msg", "查询成功");
			map.put("list", list);
			renderJson(map);
		} else {
			map.put("return_code", "fail");
			map.put("return_msg", "查询失败");
			map.put("list", aa);
			renderJson(map);
			return;
		}
		for (Record rd : list) {
			service.updatecontent(rd.get("id").toString());
		}
		renderJson(map);
	}

	// vf诊室门口等候
	public void list_roomdoor() {
		Map<String, Object> map = new HashMap<String, Object>();
		// String ip = getPara("ip");
		String ip = shardkit.getIpAddr(getRequest());
		if (StringKit.isBlank(ip)) {
			map.put("return_code", "fail");
			map.put("return_msg", "获取ip失败");
			renderJson(map);
			return;
		}
		Record record = doctor.queryByip(ip);
		if (null == record) {
			map.put("return_code", "fail");
			map.put("return_msg", "查询失败");
			renderJson(map);
			return;
		}
		String doctor_id = record.getInt("doctor_id").toString();
		if (StringKit.isBlank(doctor_id)) {
			map.put("return_code", "fail");
			map.put("return_msg", "获取医生id失败");
			renderJson(map);
			return;
		}

		List<Record> list = service.list_room_door(doctor_id);
		if (null == list) {
			map.put("return_code", "fail");
			map.put("return_msg", "查询患者等候列表失败");
			map.put("count", 0);
			renderJson(map);
			return;
		} else {
			map.put("return_code", "success");
			map.put("return_msg", "查询成功");
			map.put("list", list);
			map.put("count", list.size());
			renderJson(map);
		}
	}

	// vf诊室门口当前就诊 小屏呼叫
	public void list_roomdoor_jz() {
		Map<String, Object> map = new HashMap<String, Object>();
		// String ip=getPara("ip");
		String ip = shardkit.getIpAddr(getRequest());
		if (StringKit.isBlank(ip)) {
			map.put("return_code", "fail");
			map.put("return_msg", "获取ip失败");
			renderJson(map);
			return;
		}
		Record record = doctor.queryByip(ip);
		if (null == record) {
			map.put("return_code", "fail");
			map.put("return_msg", "查询失败");
			renderJson(map);
			return;
		}

		String login_id = record.getStr("login_id");
		if (StrKit.isBlank(login_id)) {
			map.put("return_code", "fail");
			map.put("return_msg", "获取医生工号失败");
			renderJson(map);
			return;
		}
		String doctor_id = record.getInt("doctor_id").toString();
		if (StrKit.isBlank(doctor_id)) {
			map.put("return_code", "fail");
			map.put("return_msg", "获取医生id失败");
			renderJson(map);
			return;
		}
		// 分诊台分诊模式 triage_type 1按医生 2按叫号器
		// 根据医生工号查询分诊台分诊模式
		Record rt = triage.findtypelogin_id(login_id);
		if (null == rt) {
			map.put("return_code", "fail");
			map.put("return_msg", "查询分诊台分诊模式失败");
			renderJson(map);
			return;
		}
		// 获取分诊台分针模式
		String type = rt.get("triage_type").toString();
		if ("1".equals(type)) {

			Record r = service.list_doctor_room_door_jz(doctor_id,ip);
			if (null == r) {
				map.put("return_code", "fail");
				map.put("return_msg", "查询当前就诊患者失败");
				renderJson(map);
				return;
			} else {
				map.put("return_code", "success");
				map.put("return_msg", "查询成功");
				map.put("dqjz", r);
				System.out.println(r.getStr("caller"));
				renderJson(map);
				service.updatecall(doctor_id);
			}
			
		} else {
			Record r = service.list_pager_room_door_jz(ip);
			if (null == r) {
				map.put("return_code", "fail");
				map.put("return_msg", "查询当前就诊患者失败");
				renderJson(map);
				return;
			} else {
				map.put("return_code", "success");
				map.put("return_msg", "查询成功");
				map.put("dqjz", r);
				System.out.println(r.getStr("caller"));
				renderJson(map);
				service.updatecall(doctor_id);
			}
			
		}

	}
}
